﻿using System.Buffers;
using Xc.DataAccess.Core.Extensions;
using System.Collections.Generic;
using Xc.DataAccess.Protocoals.WaterMeters.Extensions;
using Microsoft.Extensions.DependencyInjection;

namespace Xc.DataAccess.Protocoals.WaterMeters.Models.Responds
{
    /// <summary>
    /// 用户充值应答
    /// </summary>
    public class SetUserChangeRespond : MeterFrameRxDataSegmentBase
    {
        public SetUserChangeRespond(MeterFrameRx dataArea) : base(dataArea)
        {
        }

        public SetUserChangeRespond()
        {
        }

        protected override bool SetData(ref SequenceReader<byte> sequenceReader)
        {
            if (!sequenceReader.TryRead(out var behavior))
            {
                return false;
            }
            CardBehavior = (CardBehavior)behavior;
            if (!sequenceReader.TryRead(2,out var count))
            {
                return false;
            }
            ChargeCount = count.ToArray().ToUInt16LittleEndian();
            if (!sequenceReader.TryRead(4,out var buyamount))
            {
                return false;
            }
            BuyAmount = buyamount.ToArray().ToUInt32DecimalLittleEndian(10.0M);
            if (!sequenceReader.TryRead(out var unit1))
            {
                return false;
            }
            BuyAmountUnit = (MeterUnit)unit1;
            if (!sequenceReader.TryRead(4,out var cumulativeFlow))
            {
                return false;
            }
            CumulativeFlow = cumulativeFlow.ToArray().ToUInt32DecimalLittleEndian(10.0M);
            if (!sequenceReader.TryRead(out var unit2))
            {
                return false;
            }
            CumulativeFlowUnit = (MeterUnit)unit2;
            if (!sequenceReader.TryRead(4, out var remainFlow))
            {
                return false;
            }
            RemainFlow = remainFlow.ToArray().ToUInt32DecimalLittleEndian(10.0M);
            if (!sequenceReader.TryRead(out var unit3))
            {
                return false;
            }
            RemainFlowUnit = (MeterUnit)unit3;
            if (!sequenceReader.TryRead(3, out var stockFlow))
            {
                return false;
            }
            StockFlow = stockFlow.ToArray().ToUInt24DecimalLittleEndian(10.0M);
            if (!sequenceReader.TryRead(out var alarm))
            {
                return false;
            }
            AlarmFlow = alarm;
            if (!sequenceReader.TryRead(out var rssi_1))
            {
                return false;
            }
            RSSI_1 = rssi_1;
            if (!sequenceReader.TryRead(out var rssi_2))
            {
                return false;
            }
            RSSI_2 = rssi_2;
            if (!sequenceReader.TryRead(6,out var datetime))
            {
                return false;
            }
            DateTime = datetime.ToArray().ToDateTime();
            if (!sequenceReader.TryRead(2, out var meterStatus))
            {
                return false;
            }
            MeterStatus =new MeterStatus( meterStatus.ToArray());
            if (!sequenceReader.TryRead(out var random))
            {
                return false;
            }
            Random = random;
            if (!sequenceReader.TryRead(2,out var areaCode))
            {
                return false;
            }
            AreaCode = areaCode.ToArray().ToUInt16LittleEndian();
            return true;
        }
        /// <summary>
        /// 卡片行为
        /// </summary>
        public CardBehavior CardBehavior { get;private set; }
        /// <summary>
        /// 充值次数
        /// </summary>
        public ushort ChargeCount { get; private set; }
        /// <summary>
        /// 本次购买量
        /// </summary>
        public decimal BuyAmount { get; private set; }
        /// <summary>
        /// 本次购买量单位
        /// </summary>
        public MeterUnit BuyAmountUnit { get; private set; }
        /// <summary>
        /// 当前累积流量
        /// </summary>
        public decimal CumulativeFlow { get; private set; }
        /// <summary>
        /// 当前累积流量单位
        /// </summary>
        public MeterUnit CumulativeFlowUnit { get; private set; }
        /// <summary>
        /// 剩余流量
        /// </summary>
        public decimal RemainFlow { get; private set; }
        /// <summary>
        /// 剩余流量单位
        /// </summary>
        public MeterUnit RemainFlowUnit { get; private set; }
        /// <summary>
        /// 囤积量
        /// </summary>
        public decimal StockFlow { get; private set; }
        /// <summary>
        /// 报警量
        /// </summary>
        public byte AlarmFlow { get; private set; }
        /// <summary>
        /// 信号质量
        /// </summary>
        public byte RSSI_1 { get; private set; }
        /// <summary>
        /// 误码率
        /// </summary>
        public byte RSSI_2 { get; private set; }
        /// <summary>
        /// 时间格式
        /// </summary>
        public string DateTime { get; private set; }
        /// <summary>
        /// 表具状态
        /// </summary>
        public MeterStatus MeterStatus { get; private set; }
        /// <summary>
        /// 随机数
        /// </summary>
        public byte Random { get; private set; }
        /// <summary>
        /// 区域代码
        /// </summary>
        public ushort AreaCode { get; private set; }

        internal override IEnumerable<FrameCommand> ValidateCmd
        {
            get
            {
                yield return FrameCommand.CMD04RepOK;
                yield return FrameCommand.CMD04RepNG;
            }
        }
        internal override DataSegment ValidateDataSegment => DataSegment.SetUserChange;
    }
}
